// 请求通用方法
import apiMixin from '@/mixin/apiMixin';

import utils from '@/utils/index';
import envConfig from '@/config/env.js';

export default {
    mixins: [apiMixin],
    data() {
        return {
            authorized_key:  utils.handleLocalStorage.get('authorized_key'),
            host: envConfig.baseUrl,
            loading: false,
            popBtnLoad: false,
            tableData: [],
            handleShow:false,
            // 0本月 1 上月
            last: 0,
            // 虚拟用户
            virtualUser: {
                // 用户名字
                user_name :'',
                // 邀请数量
                inviter_count :'',
                // 用户头像
                avatar:"",
                // 本月收入
                income:'',
            },
            editId: null,
        };
    },
    mounted() {
        this.init();
    },
    methods: {
        init() {
            this.getInviteRankListApi();
        },
        createBid() {
            let prefix = utils.getUploadPrefix();
            let bid = prefix + '-' +  utils.createBidFn();
            this.bid = bid;
            return  bid;
        },
        /**
         *  初始化虚拟用户
         */
        initVirtualUser() {
            this.virtualUser = {
                // 用户名字
                user_name :'',
                // 邀请数量
                inviter_count :'',
                // 本月收入
                income:'',
                userId: ''
            };
        },
        changeRank() {
            this.last = this.last ? 0 : 1;
            this.getInviteRankListApi();
        },
        getInviteRankListApi() {
            let _self = this;
            this.comResquest(this.request.getInviteRankList,(res)=>{
                let list = res;
                list = list.map((item=>{
                    item.income = Number(item.income / 10000).toFixed(2);
                    return item;
                }));
                _self.tableData = list;
            },{
                last: this.last,
                load: true
            });
        },
        navUserDetail (userId) {
            if (userId == 9999) {
                this.$message.warning("系统用户");
                return;
            }
            this.$router.push(`/user/detail/${userId}`)
        },
        handlePopShow() {
            this.handleShow = !this.handleShow;
            if (!this.handleShow) {
                this.editId = null;
            }
        },
        addUserWill() {
            this.editVirtualUserDetailApi();
        },
        checkPar() {
            let { user_name,inviter_count,avatar,income } = this.virtualUser;
            if (!user_name) {
                this.$message.warning("未输入虚拟用户名称");
                return false;
            } 
            return true;
        },
        editUserWill(userIndex) {
            let curUser = this.tableData[userIndex];
            this.virtualUser = {
                // 用户名字
                user_name : curUser.name,
                // 邀请数量
                inviter_count :  '',
                // 本月收入
                income: '',
                userId: curUser.user_id
            };
            console.log(curUser.id);
            this.editId = curUser.id;
            this.handlePopShow();
        },
        addRankVirtualUserApi() {
            let resPar = this.virtualUser;
            resPar.load = "popBtnLoad";
            let _self = this;
            this.comResquest(this.request.addRankVirtualUser,(res)=>{
                _self.$message.success('添加成功！');
                _self.initVirtualUser();
                _self.init();
                _self.handlePopShow();
            },resPar);
        },
        editVirtualUserDetailApi() {
            let virtualUser = this.virtualUser;
            let resPar = {
                user_id: virtualUser.userId,
                invite_add_count: Math.abs(virtualUser.inviter_count) || 0,
                add_money: Math.abs(virtualUser.income * 10000) || 0
            };
            resPar.load = "popBtnLoad";
            resPar.id = this.editId;
            let _self = this;
            this.comResquest(this.request.editVirtualUserDetail,(res)=>{
                _self.$message.success('添加成功！');
                _self.initVirtualUser();
                _self.init();
                _self.handlePopShow();
            },resPar);
        },
        showImg(avatar) {
            let dtImg = '../../../../static/img/addImg2.png';
            if (avatar) {
                return avatar; 
            }
            return dtImg;
        },
        handleUploadWill() {
            this.popBtnLoad = true;
        },
        handleUploadSuccess(res) {
            this.popBtnLoad = false;
            let { code,data } = res;
            if (Number(code) === 0) {
                this.virtualUser.avatar = data.url;
            }
        },
    },
}